﻿using Xtee.Gen.XteeGen.XteeGenConfiguraton.Entities;
using Xtee.Gen.XteeGen.XteeGenConfiguraton.Interfaces;

namespace Xtee.Gen.XteeGen.XteeGenConfiguraton.Serializers {


    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name = "Configurations", Namespace = "http://XteeGen/XteeGenConfiguraton.xsd", Target = typeof(Configurations))]
    public class ConfigurationsSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IConfigurations clazz;
        
        public ConfigurationsSerializer(IConfigurations clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            string element = string.Empty;
            try {
                for (int x = 0; ((clazz.Item != null) 
                            && (x < clazz.Item.Count)); x = (x + 1)) {
                    if (clazz.Item[x]!=null) {
                        element = "item";
                        Writer.WriteStartElement("item");
                        Writer.WriteTypeAttribute("http://XteeGen/XteeGenConfiguraton.xsd", "XteeGenConfiguraton");

                        GetMetaData(clazz.Item[x]).NewSerializer(clazz.Item[x]).WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            catch (System.Exception ex) {
                throw new global::Xtee.Core.Exceptions.PropertyWriteException(element, this.GetType(), ex);
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            string element = string.Empty;
            try {
                element = ">>Configurations>item";
                if (Reader.LocalName=="item") {
                    clazz.Item = new System.Collections.Generic.List<IXteeGenConfiguraton>();
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if ((Reader.IsElementNil() == false)) {
                            var mapItem = this.GetMetaData<global::Xtee.Gen.XteeGen.XteeGenConfiguraton.Interfaces.IXteeGenConfiguraton>(Reader, "XteeGenConfiguraton", "http://XteeGen/XteeGenConfiguraton.xsd");
                            var item = mapItem.NewEntity<global::Xtee.Gen.XteeGen.XteeGenConfiguraton.Interfaces.IXteeGenConfiguraton>();
                            clazz.Item.Add(item);
                            if ((Reader.IsEmptyElement == false)) {
                                Reader.ReadStartElement("item");
                                mapItem.NewSerializer(item).ReadXml(Reader);
                             
                                Reader.ReadEndElement();
                            }
                            else {
                                Reader.Skip();
                            }
                        }
                        else {
                            clazz.Item.Add(null);
                            Reader.Skip();
                        }
                    }
                }
            }
            catch (System.Exception ex) {
                throw new global::Xtee.Core.Exceptions.PropertyAssignementException(element, this.GetType(), ex);
            }
        }
    }
}
